import { isChanged, createProxy } from 'proxy-compare';
import { useEffect } from 'react';

const ObjClass = (function () {
  const Obj2 = function () {
    // @ts-expect-error
    this.id = Math.random();
    console.log('custom Class constr');
  };

  Object.defineProperty(Obj2.prototype, 'getId', {
    value: function getId() {
      return Math.random();
    },
    // enumerable: true,
    // configurable: true,
    // writable: true
    // writable: false
  });
  return Obj2;
})();

const Test = () => {
  useEffect(() => {
    // @ts-expect-error
    const state = { obj: new ObjClass() };
    // const oldState = { ...state };
    const affected = new WeakMap();
    const proxy = createProxy(state, affected);

    console.log('affected', affected);

    // 访问proxy.a 收集依赖
    console.log(proxy.obj.id, proxy.obj.getId());

    // @ts-expect-error
    proxy.obj = new ObjClass();

    // 判断依赖是否修改
    console.log(isChanged(state, proxy, affected)); // false
  }, []);
  return <div />;
};

export default Test;
